package com.session; /**
 * @author wzm
 * @create 2023-06-29 11:52
 */

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/session01")
public class ServletSession01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取session对象
        HttpSession session = request.getSession();
        //往session中存储数据 基本上可以存储任何类型的数据
        session.setAttribute("key1","value1");
        //设置session存活的时间
        //正数 设置超时时间 负整数表示永不超时
        //session.setMaxInactiveInterval(60*60);

        //session.invalidate();//设置session立刻超时


        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("已经往session中保存了数据");
    }
}
